<template>
  <el-menu
    v-model:default-active="active"
    :mode="mode"
    :collapse="collapse"
    :background-color="backgroundColor"
    :text-color="textColor"
    :active-text-color="activeTextColor"
    :default-openeds="defaultOpeneds"
    :unique-opened="uniqueOpened"
    :menu-trigger="menuTrigger"
    :collapse-transition="collapseTransition"
    :router="router"
    :popper-class="popperClass"
    :popper-style="popperStyle"
    @select="handleSelect"
    @open="handleOpen"
    @close="handleClose"
  >
    <template v-for="item in items" :key="item.value">
      <el-menu-item
        v-if="!item.children"
        :index="String(item.value)"
        :disabled="item.disabled"
      >
        <el-icon v-if="item.icon">
          <component :is="item.icon" />
        </el-icon>
        <span>{{ item.label }}</span>
      </el-menu-item>
      <el-sub-menu
        v-else
        :index="String(item.value)"
        :disabled="item.disabled"
      >
        <template #title>
          <el-icon v-if="item.icon">
            <component :is="item.icon" />
          </el-icon>
          <span>{{ item.label }}</span>
          <el-icon class="el-icon--right">
            <arrow-right />
          </el-icon>
        </template>
        <el-menu-item
          v-for="child in item.children"
          :key="child.value"
          :index="String(child.value)"
          :disabled="child.disabled"
        >
          <el-icon v-if="child.icon">
            <component :is="child.icon" />
          </el-icon>
          <span>{{ child.label }}</span>
        </el-menu-item>
      </el-sub-menu>
    </template>
  </el-menu>
</template>
<script setup lang="ts">
import { ArrowRight } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import type { MenuEvents, MenuOptions } from './types'
import { ElMenu, ElMenuItem, ElSubMenu } from 'element-plus'

const props = withDefaults(defineProps<{
  modelValue?: string | number
  items: MenuOptions['items']
  mode?: MenuOptions['mode']
  collapse?: boolean
  backgroundColor?: string
  textColor?: string
  activeTextColor?: string
  defaultOpeneds?: string[]
  uniqueOpened?: boolean
  menuTrigger?: MenuOptions['menuTrigger']
  collapseTransition?: boolean
  router?: boolean
  popperClass?: string
  popperStyle?: Record<string, any>
}>(), {
  modelValue: '',
  mode: 'vertical',
  collapse: false,
  backgroundColor: '#ffffff',
  textColor: '#303133',
  activeTextColor: '#409EFF',
  defaultOpeneds: () => [],
  uniqueOpened: false,
  menuTrigger: 'hover',
  collapseTransition: true,
  router: false
})
const emit = defineEmits<MenuEvents>()
const active = ref(props.modelValue)

watch(() => props.modelValue, (val) => {
  active.value = val
})

watch(active, (val) => {
  emit('update:modelValue', val)
})

const handleSelect = (value: string | number) => {
  emit('select', value)
}

const handleOpen = (value: string[]) => {
  emit('open', value)
}

const handleClose = (value: string[]) => {
  emit('close', value)
}
</script>

<style scoped>
.el-menu {
  border-right: none;
}
</style> 